
-- NativeUIDemo.lua
-- Author: Mikael Kindborg
-- Date: 2011-02-22
-- Description: Demo of NativeUI in Lua.

-- Helper function to get the widget event type.
-- The widget event type is the int at index zero.
function maWidgetEventGetType(event)
  local widgetEvent = maEventGetData(event)
  return maBufferGetInt(widgetEvent, 0)
end

-- Helper function to get the widget handle.
-- The widget handle is the int at index one.
function maWidgetEventGetWidgetHandle(event)
  local widgetEvent = maEventGetData(event)
  return maBufferGetInt(widgetEvent, 1)
end

-- Set a table field to a value if the field is nil.
-- This is useful as a shorthand when setting widget
-- property default values.
function SetIfNil(table, field, value)
  if nil == table[field] then
    table[field] = value
  end
end

-- Widget size constants.
FILL_PARENT = "-1"
WRAP_CONTENT = "-2"

-- Function that creates a widget.
-- Property names are properties available for
-- maWidgetSetProperty, plus "type" and "parent".
function CreateWidget(properties)
  -- Create the widget.
  local widget = maWidgetCreate(properties.type)
  if WIDGET_ERROR == widget then
    return nil
  end
  -- Set properties. Properties "parent"
  -- and "type" are handled as special cases.
  for prop,value in pairs(properties) do
    if "parent" == prop then
      maWidgetAddChild(value, widget)
    elseif "type" ~= prop then
      maWidgetSetProperty(widget, prop, value)
    end
  end
  return widget
end

-- Function that creates a button widget.
function CreateButton(properties)
  properties.type = "Button"
  SetIfNil(properties, "width", FILL_PARENT)
  SetIfNil(properties, "height", WRAP_CONTENT)
  SetIfNil(properties, "textHorizontalAlignment", "center")
  SetIfNil(properties, "textVerticalAlignment", "center")
  SetIfNil(properties, "fontSize", "20.0")
  SetIfNil(properties, "fontColor", "000000")
  return CreateWidget(properties)
end
 
-- Create first screen.

Screen1 = CreateWidget { type = "Screen" }
maWidgetScreenShow(Screen1)

MainLayout1 = CreateWidget {
  type = "VerticalLayout",
  parent = Screen1,
  width = FILL_PARENT,
  height = FILL_PARENT,
  backgroundColor = "007733" }

MessageLabel =  CreateWidget {
  type = "Label",
  parent = MainLayout1,
  width = FILL_PARENT,
  height = WRAP_CONTENT, 
  fontSize = "24.0",
  fontColor = "FFFFFF",
  text = "Demo of MoSync NativeUI" }
  
ButtonSayHello = CreateButton { 
  parent = MainLayout1,
  text = "Say Hello" }

ButtonSayHi = CreateButton { 
  parent = MainLayout1,
  text = "Say Hi" }
  
ButtonOpenWebView = CreateButton { 
  parent = MainLayout1,
  text = "Open WebView" }

-- Create second screen.

Screen2 = CreateWidget { type = "Screen" }

MainLayout2 = CreateWidget {
  type = "VerticalLayout",
  parent = Screen2,
  width = FILL_PARENT,
  height = FILL_PARENT }

ButtonGoBack = CreateButton { 
  parent = MainLayout2,
  text = "Go Back" }

WebView = CreateWidget {
  type = "WebView",
  parent = MainLayout2,
  width = FILL_PARENT,
  height = FILL_PARENT,
  url = "http://www.mosync.com" }

-- Handle widget events.

System.onCustomEvent(function(event)
  if EVENT_TYPE_WIDGET == maEventGetType(event) then
    if WIDGET_EVENT_CLICKED == maWidgetEventGetType(event) then
      if ButtonSayHello == maWidgetEventGetWidgetHandle(event) then
         maWidgetSetProperty(MessageLabel, "text", "Hello World!")
      elseif ButtonSayHi == maWidgetEventGetWidgetHandle(event) then
         maWidgetSetProperty(MessageLabel, "text", "Hi there!")
      elseif ButtonOpenWebView == maWidgetEventGetWidgetHandle(event) then
	    maWidgetScreenShow(Screen2)
      elseif ButtonGoBack == maWidgetEventGetWidgetHandle(event) then
	    maWidgetScreenShow(Screen1)
	 end
    end
  end
end)

-- Things to try interactively.
-- maWidgetRemoveChild(MainLayout1, ButtonSayHi)
-- maExit(0)
